﻿using Electronic_work_order.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
namespace Electronic_work_order.Interfacess.OA_Process_application_Zhou
{
    public class Process_Application : Interface_Process_application
    {
        private readonly AppDbContext _context;

        public Process_Application(AppDbContext context)
        {
            _context = context;
        }
        //查询部门 
        public IEnumerable<Organization> GetOrganizations() {
            return _context.organizations.ToList();
        }
        //查经理级别
        public IEnumerable<Position> getManage(int OrganizationID) {
            return _context.Position.Where(a => a.ParentID == OrganizationID&a.Grade==0).ToList();
        }
        //查主管级别
        public IEnumerable<Position> getDirector(int grade)
        {
            return _context.Position.Where(a => a.Grade == grade).ToList();
        }

        //人员请假表（添加请假登记表）
        public int Add_LeaveForm(LeaveForm leave)
        {
            _context.LeaveForm.Add(leave);
            return _context.SaveChanges();
        }
   
      //修改人员请假（修改请假登记表）
        public int Update_LeaveForm(LeaveForm leave)
        {
            _context.LeaveForm.Attach(leave);
            return _context.SaveChanges();
        }

        //查询所有请假人
        public IEnumerable<LeaveForm> GetLeaves()
        {
          return  _context.LeaveForm.ToList();
        }

        //查询请假类型表
        public IEnumerable<Leave_Type> GetLeave_Types()
        {
          return  _context.Leave_Type.ToList();
        }
        //查询申请状态表
        public IEnumerable<Process_application_State> getStates()
        {
            return _context.Process_application_State.ToList();
        }

        //根据登陆成功后查询自己请假的信息
        public object Leaves_XQ(int ID)
        {
            var LeaveForms = (from leave in _context.LeaveForm
                               
                              
                                 join type in _context.Leave_Type on leave.Leave_Type_ID equals type.Leave_Type_ID
                              
                              select new
                              {
                                  leave.LeaveID,leave.Leave_Type_ID,type.Leave_Type_Name,leave.Sex,leave.AuditTime,
                                  leave.LeaveMatter
                              });
            return LeaveForms;
        }

        public int Add_Purchase_Goods(Purchase_goods goods)
        {
             _context.Purchase_Goods.Add(goods);
            return _context.SaveChanges();
        }

        public IEnumerable<Purchase_goods> GetPurchases()
        {
            throw new NotImplementedException();
        }
        public Users GetUserName(string UsersName) {
            return _context.Users.Where(a => a.UsersName == UsersName).FirstOrDefault();
        }
    }
}
